package org.example;

import org.example.dao.TestDAOImpl;
import org.example.service.TestService;
import org.example.service.TestServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) {
        //创建service对象
//        TestService service = new TestServiceImpl();
//        service.setTestDAO(new TestDAOImpl());
//        service.callDAOMethod();
        //必须对配置文件进行解析，把对象Bean元素信息存储到对应IOC容器中【配置文件方式】
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object testDAO = context.getBean("testDAO");
        System.out.println("testDAO = " + testDAO);
        System.out.println("=============================================");
        TestService testService = context.getBean("testService", TestServiceImpl.class);
        testService.callDAOMethod();
        System.out.println("----------------------------------------------");
        ///////////////////////注解方式实现IOC和DI////////////////
        ApplicationContext annotationContext = new AnnotationConfigApplicationContext("org.example");
        System.out.println(annotationContext.getBean("testServiceImpl"));
        TestService  testServiceImpl = annotationContext.getBean("testServiceImpl", TestService.class);
        testServiceImpl.callDAOMethod();
    }
}
